﻿/********************************************************************************
 * Developed by Phil Tyler (http://www.laidbackcoder.co.uk)
 * 
 * Cryptosolic is Licensed under the GNU Lesser General Public License (LGPL)
 * http://cryptosolic.codeplex.com/license
 * *****************************************************************************/
using Cryptosolic.Security.Hashing;
using NUnit.Framework;
using System.Text;

namespace Cryptosolic.Security.Tests
{
    /// <summary>
    /// Base Methods to Test a Hashing Algorithm
    /// </summary>
    public class HashingTestsBase
    {
        public IHashingAlgorithm hashAlgorithm = null;


        public void assert_HashData_OutputHashDifferentFromInput()
        {
            byte[] initialValue = Encoding.UTF8.GetBytes("this.is.a.test");
            byte[] actual = hashAlgorithm.HashData(initialValue).ToBytes();

            CollectionAssert.AreNotEqual(initialValue, actual);
        }


        public void assert_HashData_OutputHashRepeatable()
        {
            byte[] initialValue = Encoding.UTF8.GetBytes("this.is.a.test");

            byte[] actual1 = hashAlgorithm.HashData(initialValue).ToBytes();
            byte[] actual2 = hashAlgorithm.HashData(initialValue).ToBytes();

            CollectionAssert.AreEqual(actual1, actual2);
        }


        public void assert_HashData_OutputHashDifferentWithTwoSimilarInputs()
        {
            byte[] initialValue1 = Encoding.UTF8.GetBytes("this.is.a.test");
            byte[] initialValue2 = Encoding.UTF8.GetBytes("this.is.b.test");

            byte[] actual1 = hashAlgorithm.HashData(initialValue1).ToBytes();
            byte[] actual2 = hashAlgorithm.HashData(initialValue2).ToBytes();

            CollectionAssert.AreNotEqual(actual1, actual2);
        }


        public void assert_HashData_OutputHashSameAsKnownHash(string expected)
        {
            byte[] initialValue = Encoding.UTF8.GetBytes("this.is.a.test");
            string actual = hashAlgorithm.HashData(initialValue).ToHex();
            
            Assert.AreEqual(expected, actual);
        }
    }
}
